package cn.ctvonline.android.kaopu.widget;

import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import cn.ctvonline.android.kaopu.R;

/**
 * Created by dujian on 15/12/2.
 */
public class ChooserWindow extends PopupWindow {
    private View contentView;
    private Context context;
    private RecyclerView valueRv;
    private RecyclerView.Adapter valueAdapter;

    public ChooserWindow(Context context) {
        super(context);
        this.context = context;
        createView();
    }

    public ChooserWindow(Context context, RecyclerView.Adapter adapter) {
        this(context);
        this.valueAdapter = adapter;
    }

    private void createView() {
        contentView = LayoutInflater.from(context).inflate(R.layout.dialog_value_chooser, null, false);
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        m.getDefaultDisplay().getSize(point);
        int screenHeight = point.y;
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight((int) (screenHeight * 0.5));
        setOutsideTouchable(false);
        setClippingEnabled(true);
        setBackgroundDrawable(context.getResources().getDrawable(R.color.white));
        setContentView(contentView);
        valueRv = (RecyclerView) contentView.findViewById(R.id.dialog_value_rv);
        valueRv.setAdapter(valueAdapter);
    }

    public void show(View parentView) {
        if (!isShowing()) {
            showAsDropDown(parentView, parentView.getLayoutParams().width / 2, 10);
        }
    }
}
